﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SynapseGaming.LightingSystem.Lights;
using SynapseGaming.LightingSystem.Shadows;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class AmbientLightSunBurn : Component
    {
        public AmbientLight AmbientLight;
        public Actor Actor;
        public Renderer Renderer;
        public AmbientLightSunBurn(Renderer renderer, GameScreen screen)
            : base(screen)
        {
            this.Renderer = renderer;
            AmbientLight = new AmbientLight();
            Actor = new Actor("Engine\\Light\\Model\\model_light");
            Actor.Parent = AmbientLight;
            Actor.Scale = new Vector3(0.5f);
            Actor.ComponentParent = this;
            AmbientLight.Intensity = 0.1f;

            Renderer.SubmitLight(AmbientLight);
            Renderer.SubmitActor(Actor);
        }
        public AmbientLightSunBurn()
        {
        }

        public override void Update()
        {
            base.Update();

            Actor.Update();         
        }

        public override void Draw()
        {
            base.Draw();

            Actor.Draw();
        }

        public override void SerializeData()
        {
            base.SerializeData();

            Data.AddData("Light.DiffuseColor", AmbientLight.DiffuseColor);
            Data.AddData("Light.Enabled", AmbientLight.Enabled);
            Data.AddData("Light.Intensity", AmbientLight.Intensity);

            Data.AddData("Renderer", this.Renderer.Name);

            Actor.SerializeData(Data);
        }
        public override void DeserializeData()
        {
            base.DeserializeData();

            AmbientLight = new AmbientLight();
            AmbientLight.DiffuseColor = Data.GetData<Vector3>("Light.DiffuseColor");
            AmbientLight.Enabled = Data.GetData<bool>("Light.Enabled");
            AmbientLight.Intensity = Data.GetData<float>("Light.Intensity");

            Actor = new Actor("Engine\\Light\\Model\\model_light");
            Actor.Parent = AmbientLight;
            Actor.ComponentParent = this;
            Actor.DeserializeData(Data);

            string rendererName = Data.GetData<string>("Renderer");
            if (!string.IsNullOrEmpty(rendererName))
            {
                if (EngineSystem.Renderers.ContainsKey(rendererName))
                {
                    this.Renderer = EngineSystem.Renderers[rendererName];
                    Renderer.SubmitLight(AmbientLight);
                    Renderer.SubmitActor(Actor);
                }
            }
            else
            {
                if (EngineSystem.Renderers.Count > 0)
                {
                    this.Renderer = EngineSystem.Renderers.Values.First();
                    Renderer.SubmitLight(AmbientLight);
                    Renderer.SubmitActor(Actor);
                }
            }
        }
    }
}
